
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class TcpServer {
	
    private static final int TCP_SERVER_PORT = 21111;
    /** Called when the activity is first created. */
	public static void main(String[] args) throws IOException {

   
        runTcpServer();
    }

    private static void runTcpServer() throws IOException  {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(TCP_SERVER_PORT);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + TCP_SERVER_PORT);
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
				new InputStreamReader(
				clientSocket.getInputStream()));
        String inputLine, outputLine;
        Protocol kkp = new Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("bye"))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
    

}